/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "LoginScene.h"

#include "GlobalVariables.h"
#include "NetManager.h"

namespace PQClient
{
	LoginScene::LoginScene(Ogre::RenderWindow* win) : SceneObject(win),
		_status(Login_Select_Server),_relogin(false)
	{
		loadConfig();
	}

	void LoginScene::loadConfig()
	{
		//
		//_serverAddr _serverPort

	}

	void LoginScene::loadScene()
	{
		SceneObject::loadScene();

		this->getRenderProperty().setBackground(Ogre::ColourValue(1,0,0));
	}

	void LoginScene::update(float deltaT)
	{
		//update the scene
		SceneObject::update(deltaT);

		//update login related 
		switch(_status){
		case Login_Select_Server:
		{
			Event* evt=new Event(Event::Event_Server_Change,this);
			this->dispatchEvent(evt);
			delete evt;
			break;
		}
		case Login_Debug_Setting:
			setStatus(Login_Disconnect);
			break;
		case Login_Disconnect:
			this->pushDebugString("Connect to login server %s:%d...", _serverAddr, _serverPort);
			setStatus(Login_Connecting);
			NetManager::getSingleton().connect(_serverAddr,_serverPort);
			break;
		case Login_Connecting:
			break;
		case Login_Connect_OK:
			//Let user login
			setStatus(Login_Account_Requesting);
			break;
		case Login_Connect_Fail:
			NetManager::getSingleton().close();
			setStatus(Login_Select_Server);
			break;
		case Login_Account_Requesting:
			break;
		case Login_Account_Begin_Requesting:
			break;
		case Login_Account_OK:
		{
			//User account verified
			//Keep globals
			Globals.setInt32(GlobalVariables::Int32_LoginAreaId,_areaId);
			Globals.setInt32(GlobalVariables::Int32_LoginServerId,_serverId);
			Globals.setInt32(GlobalVariables::Int32_LoginMode,(int32)Login_First_Login);
			

			Event* evt=new Event(Event::Event_Login_Success,this);
			this->dispatchEvent(evt);
			delete evt;
			break;
		}
		default:
			break;
		}
	}
}
